package service

import (
	"context"
	"fmt"

	"gitee.com/wangshh_123/cross-pay-go/src/models"
	"gitee.com/wangshh_123/cross-pay-go/src/utils/common/i18n"
	"gorm.io/gorm"
)

// CardFeeService 卡费服务
type CardFeeService struct {
	db *gorm.DB
}

// NewCardFeeService 创建卡费服务
func NewCardFeeService(db *gorm.DB) *CardFeeService {
	return &CardFeeService{
		db: db,
	}
}

// GetCardFee 获取卡费配置
func (s *CardFeeService) GetCardFee(ctx context.Context, cardType, cardFormFactor, currency string) (*models.CardFeeResponse, error) {
	var config models.CardFeeConfig

	// 查询卡费配置
	err := s.db.WithContext(ctx).
		Where("card_type = ? AND card_form_factor = ? AND currency = ? AND status = ?",
			cardType, cardFormFactor, currency, "active").
		First(&config).Error

	if err != nil {
		if err == gorm.ErrRecordNotFound {
			msg := i18n.GetMessage("card_fee_config_not_found", i18n.CN)
			return nil, fmt.Errorf(msg, cardType, cardFormFactor, currency)
		}
		msg := i18n.GetMessage("card_fee_config_query_failed", i18n.CN)
		return nil, fmt.Errorf(msg)
	}

	// 转换为响应格式
	response := &models.CardFeeResponse{
		CardType:       config.CardType,
		CardFormFactor: config.CardFormFactor,
		Currency:       config.Currency,
		OpenCardFee:    config.OpenCardFee,
		MonthlyFee:     config.MonthlyFee,
		ExchangeRate:   config.ExchangeRate,
		RefundRatio:    config.RefundRatio,
	}

	return response, nil
}

// GetCardFees 获取所有卡费配置
func (s *CardFeeService) GetCardFees(ctx context.Context) ([]models.CardFeeResponse, error) {
	var configs []models.CardFeeConfig

	// 查询所有启用的卡费配置
	err := s.db.WithContext(ctx).
		Where("status = ?", "active").
		Find(&configs).Error

	if err != nil {
		msg := i18n.GetMessage("card_fee_config_list_failed", i18n.CN)
		return nil, fmt.Errorf(msg)
	}

	// 转换为响应格式
	responses := make([]models.CardFeeResponse, len(configs))
	for i, config := range configs {
		responses[i] = models.CardFeeResponse{
			CardType:       config.CardType,
			CardFormFactor: config.CardFormFactor,
			Currency:       config.Currency,
			OpenCardFee:    config.OpenCardFee,
			MonthlyFee:     config.MonthlyFee,
			ExchangeRate:   config.ExchangeRate,
			RefundRatio:    config.RefundRatio,
		}
	}

	return responses, nil
}

// CreateCardFee 新增卡费配置
func (s *CardFeeService) CreateCardFee(ctx context.Context, config *models.CardFeeConfig) error {
	return s.db.WithContext(ctx).Create(config).Error
}

// UpdateCardFee 更新卡费配置
func (s *CardFeeService) UpdateCardFee(ctx context.Context, id int64, update map[string]interface{}) error {
	return s.db.WithContext(ctx).Model(&models.CardFeeConfig{}).Where("id = ?", id).Updates(update).Error
}

// DeleteCardFee 删除卡费配置
func (s *CardFeeService) DeleteCardFee(ctx context.Context, id int64) error {
	return s.db.WithContext(ctx).Delete(&models.CardFeeConfig{}, id).Error
}

// GetCardFeeByID 根据ID获取卡费配置
func (s *CardFeeService) GetCardFeeByID(ctx context.Context, id int64) (*models.CardFeeConfig, error) {
	var config models.CardFeeConfig
	err := s.db.WithContext(ctx).First(&config, id).Error
	if err != nil {
		return nil, err
	}
	return &config, nil
}
